package com.Lab427.workflow.controller;

import com.Lab427.workflow.common.dtos.ResponseResult;
import com.Lab427.workflow.dtos.WorkflowData;
import com.Lab427.workflow.entity.WorkflowEdge;
import com.Lab427.workflow.service.DAGExecutor.DAGExecutorService;
import com.Lab427.workflow.service.WorkflowEdgeService;
import com.Lab427.workflow.service.WorkflowNodeService;
import com.Lab427.workflow.service.WorkflowService;
import com.Lab427.workflow.entity.Workflow;
import com.Lab427.workflow.entity.WorkflowNode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Yue
 * @date:2025/8/26 19:35
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/workflow")
public class workflowController {

    @Autowired
    private DAGExecutorService dagExecutorService;

    @Autowired
    private WorkflowService workflowService;
    @Autowired
    private WorkflowNodeService nodeService;
    @Autowired
    private WorkflowEdgeService edgeService;

    /**
     * @author Yue
     * @date:2025/8/26 19:46
    {
    "workflow": {
    "userId": 1,
    "name": "find_place",
    "description": "网点选址"
    },
    "nodes": [
    {
    "nodeId": 1,
    "type": "start",
    "name": "开始节点",
    "config": {"key":"value1"}
    },
    {
    "nodeId": 2,
    "type": "task",
    "name": "任务节点",
    "config": {"key":"value2"}
    },
    {
    "nodeId": 3,
    "type": "end",
    "name": "结束节点",
    "config": {"key":"value3"}
    }
    ],
    "edges": [
    {
    "fromNodeId": 1,
    "toNodeId": 2
    },
    {
    "fromNodeId": 2,
    "toNodeId": 3
    }
    ]
    }

     *
     */
    @RequestMapping(value = "/create")
    @Transactional
    public ResponseResult create(@RequestBody WorkflowData workflowdata){
        Workflow workflow = workflowdata.getWorkflow();
        List<WorkflowNode> nodes = workflowdata.getNodes();
        List<WorkflowEdge> edges = workflowdata.getEdges();

        workflowService.save(workflow);
        Long workflowId = workflow.getId();
        // 保存节点
        for (WorkflowNode node : workflowdata.getNodes()) {
            node.setWorkflowId(workflowId);
        }
        nodeService.saveBatch(workflowdata.getNodes());

        // 保存边
        for (WorkflowEdge edge : workflowdata.getEdges()) {
            edge.setWorkflowId(workflowId);
        }
        edgeService.saveBatch(workflowdata.getEdges());

        return ResponseResult.okResult(new String("ok"));
    }

    @RequestMapping(value = "/execute")
    @Transactional
    public ResponseResult execute(@PathVariable Long id){
    // 根据 workflowId 查询 workflow
        Workflow workflow = workflowService.getById(id);

        // 查询节点和边
        List<WorkflowNode> nodes = nodeService.list(
                new QueryWrapper<WorkflowNode>().eq("workflow_id", id)
        );
        List<WorkflowEdge> edges = edgeService.list(
                new QueryWrapper<WorkflowEdge>().eq("workflow_id", id)
        );

        // 封装返回
        Map<String, Object> result = new HashMap<>();
        result.put("workflow", workflow);
        result.put("nodes", nodes);
        result.put("edges", edges);

        ResponseResult responseResult = dagExecutorService.execute(result);
        return responseResult.ok("ok");


    }
}
